package org.example.order.controller;

import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.example.contract.domain.ContractProduct;
import org.example.contract.service.IContractProductService;
import org.example.utils.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import org.example.order.domain.Order;
import org.example.order.service.IOrderService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author 小孟微信：xiaomengcode
 * @date 2024-04-12
 */
@Controller
@RequestMapping("/order/order")
public class OrderController extends BaseController
{
    private String prefix = "order/order";

    @Autowired
    private IOrderService orderService;

    @Autowired
    private IContractProductService contractProductService;

    @GetMapping()
    public String order(String contractType,String contractId,String supplierId,ModelMap mmp)
    {
        mmp.put("contractType",contractType);
        mmp.put("contractId",contractId);
        mmp.put("supplierId",supplierId);
        return prefix + "/order";
    }

    /**
     * 查询订单列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @RequiresPermissions("order:order:export")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Order order)
    {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 新增订单
     */
    @GetMapping("/add")
    public String add(String contractId,String contractType, ContractProduct contractProduct,String  supplierId,ModelMap mmap)
    {
        List<ContractProduct> list = contractProductService.selectContractProductList(contractProduct);
        mmap.put("contractId", contractId);
        mmap.put("contractType", contractType);
        mmap.put("supplierId",supplierId);
        mmap.put("list",list);
        return prefix + "/add";
    }

    /**
     * 新增保存订单
     */
    @RequiresPermissions("order:order:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Order order)
    {

        order.setOrderNo(getCurrentOrderNo(order));
        int i=orderService.insertOrder(order);
        return toAjax(i);
    }

    private String getCurrentOrderNo(Order order) {
        Order orderParams=new Order();
        orderParams.setCreateTime(DateUtils.getNowDate());
        orderParams.setOrderType(order.getOrderType());
        startPage();
        List<Order> list = orderService.selectOrderList(orderParams);
        TableDataInfo dataTable = getDataTable(list);
        Long total =dataTable.getTotal()+1L;
        String s2 = String.format("%04d", total);
        String orderNo = null;
        if(Util.CONTRACT_TYPE_SELL.equals(order.getOrderType().toString())){
            //销售
            orderNo =  "DN"+DateUtils.dateTimeNow("yyMMdd")+s2;
        }else{
            //采购
            orderNo =  "DS"+DateUtils.dateTimeNow("yyMMdd")+s2;
        }
        return orderNo;
    }

}
